package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.Activity;
import com.wuming.house.model.Decorate;
import com.wuming.house.service.ActivityService;
import com.wuming.house.service.DecorateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/27 18:51
 * @Description:
 */
@Controller
@RequestMapping("/web/activity")
public class ActivityController extends BaseController{

    @Autowired
    private ActivityService activityService;

    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false)String title, @RequestParam(required = false)String source,
                             @RequestParam(defaultValue = "1")int pageIndex, @RequestParam(defaultValue = "10")int pageSize){
        logger.info("准备查询所有活动");
        try {
            List<Activity> activities = activityService.getActivitiesByPage(title,source,pageIndex,pageSize);
            int total = activityService.getTotal(title,source);
            return CommonResult.createSuccessResult(activities,total,"查询所有活动成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
        }
        return CommonResult.createFailureResult("查询所有活动失败");
    }

    @RequestMapping("/toManageActivity")
    public String toManageActivity(){
        return "/tools/manageActivity";
    }

    @RequestMapping("/toEditActivity/{type}/{id}")
    public String toEditActivity(@PathVariable Integer type, @PathVariable Integer id, Model model){
        if (type==2){
            if (null!=id){
                model.addAttribute("activity",activityService.getActivityById(id));
            }
        }
        return "/tools/editActivity";
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public JsonResult edit(Activity activity){
        if (null == activity.getId()) {
            activity.setCreateDatetime(new Date());
            activityService.saveActivity(activity);
            return new JsonResult(null,"添加活动成功");
        } else {
            activityService.modifyActivity(activity);
            return new JsonResult(null,"更新活动成功");
        }
    }

    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public JsonResult uploadImage(){
        return new JsonResult("1/2/3","",true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Integer id){
        logger.info("准备删除id:{}的活动",id);
        try {
            activityService.delete(id);
            return new JsonResult(null,"删除成功",true);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
            return new JsonResult(null,"删除失败",false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request){
        try {
            String path = this.uploadImage(file,"activity",1);
            logger.info("文件成功上传到指定目录下,文件目录:{},",path);
            return new JsonResult(path,"上传成功",true);
        }catch (IOException ie){
            ie.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult("","上传失败",false);
    }

    @ResponseBody
    @RequestMapping("/get/{activityId}")
    public JsonResult get(@PathVariable Integer activityId){
        try {
            Activity activity = activityService.getActivityById(activityId);
            return new JsonResult(activity.getUrl(),"",true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(null,"",false);
    }

}
